import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import { Store } from 'vuex'

import stores from '@/store'
import localCache from '@/utils/cache'

const store: Store<any> = stores
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/main/index/9'
  },
  {
    path: '/main',
    name: 'main',
    props: true,
    component: () => import('@/views/main/index.vue'),
    children: [
      {
        path: 'index/:i',
        name: 'index',
        props: true,
        component: () => import('@/views/main/home/index.vue'),
        meta: {
          title: '医生首页',
          requireAuth: true
        }
      },
      {
        path: 'message/:i',
        name: 'message',
        props: true,
        component: () => import('@/views/main/message/index.vue'),
        meta: {
          title: '我的信息',
          requireAuth: true
        }
      },
      {
        path: 'assistant/:i',
        name: 'assistant',
        props: true,
        component: () => import('@/views/main/assistant/index.vue'),
        meta: {
          title: '我的助理',
          requireAuth: true
        }
      },
      {
        path: 'patient/:i',
        name: 'patient',
        props: true,
        component: () => import('@/views/main/patient/index.vue'),
        meta: {
          title: '我的病人',
          requireAuth: true
        }
      },
      {
        path: 'article/:i',
        name: 'article',
        props: true,
        component: () => import('@/views/main/article/index.vue'),
        meta: {
          title: '我的文章',
          requireAuth: true
        }
      },
      {
        path: 'video/:i',
        name: 'video',
        props: true,
        component: () => import('@/views/main/video/index.vue'),
        meta: {
          title: '我的视频',
          requireAuth: true
        }
      },
      {
        path: 'prescription/:i',
        name: 'prescription',
        props: true,
        component: () => import('@/views/main/prescription/index.vue'),
        meta: {
          title: '电子处方',
          requireAuth: true
        }
      },
      {
        path: 'edit/:i',
        name: 'edit',
        props: true,
        component: () => import('@/views/main/edit/index.vue'),
        meta: {
          title: '信息修改',
          requireAuth: true
        }
      }
    ]
  },
  {
    path: '/login/:i',
    name: 'login',
    props: true,
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "login" */ '@/views/login/index.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
router.beforeEach((to, from, next) => {
  const title = to.meta?.title as string | undefined
  if (title) {
    document.title = title
  }
  if (to.params.i) {
    localCache.setCache('i', to.params.i)
  }
  if (to.matched.some((record) => record.meta.requireAuth)) {
    // this route requires auth, check if logged in
    // if not, redirect to login page.
    const uniacid = to.params.i
    const token = store.state.user.token

    if (!token) {
      if (to.name !== 'login' || to.path !== '/404') {
        localStorage.setItem('redirect', to.fullPath)
      }
      next(`/login/${uniacid}`)
    } else {
      next()
      // if (to.name != 'editInfo' && !to.query.qr_code) {
      //   if (!store.state.user.userInfo.doctor_name) {
      //     next({
      //       path: `/editInfo/${uniacid}`
      //     })
      //   } else {
      //     next()
      //   }
      // } else {
      //   next()
      // }
    }
  } else {
    next() // 确保一定要调用 next()
  }
})
export default router
